# define _CRT_SECURE_NO_WARNINGS 1

//// strings and c-strings
//#include <iostream>
//#include <cstring>
//#include <string>
//using namespace std;
//int main()
//{
//    std::string str("hello world!");
//
//    char* cstr = new char[str.length() + 1];
//    strcpy(cstr, str.c_str());
//    
//    printf("%s\n", str.c_str());
//    printf("%s\n", cstr);
//    return 0;
//}

//// string::data
//#include <iostream>
//#include <string>
//#include <cstring>
//using namespace std;
//int main()
//{
//    int length;
//
//    std::string str = "Test string";
//    const char* cstr = "Test string";
//
//    if (str.length() == std::strlen(cstr))
//    {
//        cout << "str and cstr have the same length.\n";
//
//        if (memcmp(cstr, str.data(), str.length()) == 0)
//            cout << "str and cstr have the same content.\n";
//    }
//    return 0;
//}

//// string::copy
//#include <iostream>
//#include <string>
//using namespace std;
//int main()
//{
//	char buffer[20];
//	string str("Test string...");
//	size_t length = str.copy(buffer, 6, 5);
//	buffer[length] = '\0';
//	cout << "buffer contains: " << buffer << '\n';
//	return 0;
//}


//// string::find
//#include <iostream>       // std::cout
//#include <string>         // std::string
//using namespace std;
//int main()
//{
//    string str("There are two needles in this haystack with needles.");
//    string str2("needle");
//
//    // different member versions of find in the same order as above:
//    size_t found = str.find(str2);
//    if (found != std::string::npos)
//        cout << "first 'needle' found at: " << found << '\n';
//
//    found = str.find("needles are small", found + 1, 6);
//    if (found != std::string::npos)
//        cout << "second 'needle' found at: " << found << '\n';
//
//    found = str.find("haystack");
//    if (found != std::string::npos)
//        cout << "'haystack' also found at: " << found << '\n';
//
//    found = str.find('.');
//    if (found != std::string::npos)
//        cout << "Period found at: " << found << '\n';
//
//    // let's replace the first needle:
//    str.replace(str.find(str2), str2.length(), "preposition");
//    cout << str << '\n';
//
//    return 0;
//}

//// string::rfind
//#include <iostream>
//#include <string>
//#include <cstddef>
//using namespace std;
//int main()
//{
//    string str("The sixth sick sheik's sixth sheep's sick.");
//    string key("sixth");
//
//    size_t found = str.rfind(key);
//    if (found != string::npos)
//        str.replace(found, key.length(), "seventh");
//
//    cout << str << '\n';
//
//    return 0;
//}


//// string::find_first_of
//#include <iostream>       // std::cout
//#include <string>         // std::string
//#include <cstddef>        // std::size_t
//using namespace std;
//int main()
//{
//    string str("Please, replace the vowels in this sentence by asterisks.");
//    size_t found = str.find_first_of("aeiou");
//    while (found != std::string::npos)
//    {
//        str[found] = '*';
//        found = str.find_first_of("aeiou", found + 1);
//    }
//
//    cout << str << '\n';
//
//    return 0;
//}


//// string::find_last_of
//#include <iostream>       // std::cout
//#include <string>         // std::string
//#include <cstddef>         // std::size_t
//using namespace std;
//
//void SplitFilename(const std::string& str)
//{
//	cout << "Splitting: " << str << '\n';
//	size_t found = str.find_last_of("/\\");
//	cout << " path: " << str.substr(0, found) << '\n';
//	cout << " file: " << str.substr(found + 1) << '\n';
//}
//
//int main()
//{
//	string str1("/usr/bin/man");
//	string str2("c:\\windows\\winhelp.exe");
//
//	SplitFilename(str1);
//	SplitFilename(str2);
//
//	return 0;
//}


//// string::find_first_not_of
//#include <iostream>       // std::cout
//#include <string>         // std::string
//#include <cstddef>        // std::size_t
//using namespace std;
//int main()
//{
//    string str("look for non-alphabetic characters...");
//
//    size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
//
//    if (found != string::npos)
//    {
//        cout << "The first non-alphabetic character is " << str[found];
//        cout << " at position " << found << '\n';
//    }
//
//    return 0;
//}


//// string::find_last_not_of
//#include <iostream>       // std::cout
//#include <string>         // std::string
//#include <cstddef>        // std::size_t
//using namespace std;
//int main()
//{
//    string str("Please, erase trailing white-spaces   \n");
//    string whitespaces(" \t\f\v\n\r");
//
//    size_t found = str.find_last_not_of(whitespaces);
//    if (found != string::npos)
//        str.erase(found + 1);
//    else
//        str.clear();            // str is all whitespace
//
//    cout << '[' << str << "]\n";
//
//    return 0;
//}


//// string::substr
//#include <iostream>
//#include <string>
//
//int main()
//{
//    std::string str = "We think in generalities, but we live in details.";
//    // (quoting Alfred N. Whitehead)
//
//    std::string str2 = str.substr(3, 5);     // "think"
//
//    std::size_t pos = str.find("live");      // position of "live" in str
//
//    std::string str3 = str.substr(pos);     // get from "live" to the end
//
//    std::cout << str2 << ' ' << str3 << '\n';
//
//    return 0;
//}

// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1("green apple");
    string str2("red apple");

    if (str1.compare(str2) != 0)
        cout << str1 << " is not " << str2 << '\n';

    if (str1.compare(6, 5, "apple") == 0)
        cout << "still, " << str1 << " is an apple\n";

    if (str2.compare(str2.size() - 5, 5, "apple") == 0)
        cout << "and " << str2 << " is also an apple\n";

    if (str1.compare(6, 5, str2, 4, 5) == 0)
        cout << "therefore, both are apples\n";

    return 0;
}